package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.operator.Equal;
import com.ejlchina.searcher.util.MapUtils;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Employee;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author tzl
 * @Date 2023-07-23 11:42:14  星期日
 * @Description: 用户购物车业务实现类
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    public ShoppingCartMapper shoppingCartMapper;

    @Autowired
    public DishMapper dishMapper;

    @Autowired
    public SetmealMapper setmealMapper;


    @Autowired
    private BeanSearcher beanSearcher;


    /**
     * 查询用户购物车信息
     * @return
     */
    @Override
    public List<ShoppingCart> selectShoppingCart() {
        Long userId = BaseContext.getCurrentUserId();
        Map<String, Object> build = MapUtils.builder()
                .field(ShoppingCart::getUserId,userId).op(Equal.class)
                .build();
        return beanSearcher.searchAll(ShoppingCart.class, build);
    }

    /**
     * 用户添加购物车
     * @param dto 商品信息
     * @return
     */
    @Override
    public int insertShoppingCart(ShoppingCartDTO dto) {
        int result;
        Long userId = BaseContext.getCurrentUserId();
        Map<String, Object> build = MapUtils.builder()
                .field(ShoppingCart::getUserId,userId).op(Equal.class)
                .field(ShoppingCart::getSetmealId,dto.getSetmealId()).op(Equal.class)
                .field(ShoppingCart::getDishId,dto.getDishId()).op(Equal.class)
                .field(ShoppingCart::getDishFlavor,dto.getDishFlavor()).op(Equal.class)
                .build();
        ShoppingCart shoppingCart = beanSearcher.searchFirst(ShoppingCart.class, build);
        if (Objects.nonNull(shoppingCart)){
            Long dishId = shoppingCart.getDishId();
            if (dishId != null && dishId > 0){
                Map<String, Object> build1 = MapUtils.builder().field(Dish::getId, dishId).op(Equal.class).build();
                Dish dish = beanSearcher.searchFirst(Dish.class, build1);
                //shoppingCart.setAmount(shoppingCart.getAmount().add(dish.getPrice()));
            }else {
                Long setmealId = dto.getSetmealId();
                Map<String, Object> build1 = MapUtils.builder().field(Setmeal::getId, setmealId).op(Equal.class).build();
                Setmeal setmeal = beanSearcher.searchFirst(Setmeal.class, build1);
                //shoppingCart.setAmount(shoppingCart.getAmount().add(setmeal.getPrice()));
            }
            //购物车商品如果存储，则商品数量+1
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCart.setUpdateBy(BaseContext.getCurrentUserName());
            shoppingCart.setUpdateTime(DateUtils.getNowDate());
            result = shoppingCartMapper.updateById(shoppingCart);
        }else {
            /*
              购物车商品如果不存在，则新增购物车
             */
            //判断本次添加到购物车的信息是菜品还是套餐
            Long dishId = dto.getDishId();
            //添加购物车信息
            ShoppingCart shoppingCart1 = new ShoppingCart();
            if (dishId != null && dishId > 0) {
                Map<String, Object> build1 = MapUtils.builder().field(Dish::getId, dishId).op(Equal.class).build();
                Dish dish = beanSearcher.searchFirst(Dish.class, build1);
                shoppingCart1.setName(dish.getName());
                shoppingCart1.setImage(dish.getImage());
                shoppingCart1.setAmount(dish.getPrice());
                shoppingCart1.setDishId(dishId);
                shoppingCart1.setDishFlavor(dto.getDishFlavor());
            }else {
                Long setmealId = dto.getSetmealId();
                Map<String, Object> build1 = MapUtils.builder().field(Setmeal::getId, setmealId).op(Equal.class).build();
                Setmeal setmeal = beanSearcher.searchFirst(Setmeal.class, build1);
                shoppingCart1.setName(setmeal.getName());
                shoppingCart1.setImage(setmeal.getImage());
                shoppingCart1.setAmount(setmeal.getPrice());
                shoppingCart1.setSetmealId(setmealId);
            }
            shoppingCart1.setNumber(1);
            shoppingCart1.setUserId(userId);
            shoppingCart1.setCreateBy(BaseContext.getCurrentUserName());
            shoppingCart1.setCreateTime(DateUtils.getNowDate());
            shoppingCart1.setUpdateBy(BaseContext.getCurrentUserName());
            shoppingCart1.setUpdateTime(DateUtils.getNowDate());
            result = shoppingCartMapper.insert(shoppingCart1);
        }
        return result;
    }

    @Override
    public int updateShoppingCart(ShoppingCartDTO dto) {
        int result;
        Long userId = BaseContext.getCurrentUserId();
        Map<String, Object> build = MapUtils.builder()
                .field(ShoppingCart::getUserId,userId).op(Equal.class)
                .field(ShoppingCart::getSetmealId,dto.getSetmealId()).op(Equal.class)
                .field(ShoppingCart::getDishId,dto.getDishId()).op(Equal.class)
                .field(ShoppingCart::getDishFlavor,dto.getDishFlavor()).op(Equal.class)
                .build();
        ShoppingCart shoppingCart = beanSearcher.searchFirst(ShoppingCart.class, build);
        Integer number = shoppingCart.getNumber();
        Long dishId = dto.getDishId();
        if (number > 1) {
            if (dishId != null && dishId >0) {
                Map<String, Object> build1 = MapUtils.builder().field(Dish::getId, dishId).op(Equal.class).build();
                Dish dish = beanSearcher.searchFirst(Dish.class, build1);
               // shoppingCart.setAmount(shoppingCart.getAmount().subtract(dish.getPrice()));
            }else {
                Long setmealId = dto.getSetmealId();
                Map<String, Object> build1 = MapUtils.builder().field(Setmeal::getId, setmealId).op(Equal.class).build();
                Setmeal setmeal = beanSearcher.searchFirst(Setmeal.class, build1);
                //shoppingCart.setAmount(shoppingCart.getAmount().subtract(setmeal.getPrice()));
            }
            shoppingCart.setNumber(shoppingCart.getNumber() -1);
            shoppingCart.setUpdateBy(BaseContext.getCurrentUserName());
            shoppingCart.setUpdateTime(DateUtils.getNowDate());
            result = shoppingCartMapper.updateById(shoppingCart);
        }else {
            UpdateWrapper<ShoppingCart> wrapper = new UpdateWrapper<>();
            wrapper.eq("user_id",userId);
            if(dishId != null && dishId > 0){
                wrapper.eq("dish_id",dishId);
                String dishFlavor = dto.getDishFlavor();
                if (Objects.nonNull(dishFlavor)){
                    wrapper.eq("dish_flavor",dishFlavor);
                }
            }else {
                Long setmealId = dto.getSetmealId();
                wrapper.eq("setmeal_id",setmealId);
            }
            wrapper.set("del_flag","2");
            wrapper.set("update_time", DateUtils.getNowDate());
            wrapper.set("update_by", BaseContext.getCurrentUserName());
            return shoppingCartMapper.update(null,wrapper);
        }

        return result;
    }

    /**
     * 用户清空购物车
     * @return
     */
    @Override
    public int deleteShoppingCart() {
        Long userId = BaseContext.getCurrentUserId();
        UpdateWrapper<ShoppingCart> wrapper = new UpdateWrapper<>();
        wrapper.in("user_id",userId);
        wrapper.set("del_flag","2");
        wrapper.set("update_time", DateUtils.getNowDate());
        wrapper.set("update_by", BaseContext.getCurrentUserName());
        return shoppingCartMapper.update(null,wrapper);
    }
}
